package edu.rmit.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JTextArea;

import edu.rmit.service.IChatService;
import edu.rmit.util.PromotDialog;

/**
 * The class is to deal with when a user clicked the send chat message button
 * send. And the chat message will also send to all the participant of this group.
 * 
 * @author Thaw Zin
 * @version 1.0
 */
public class ListenerSendLiveChatMsg implements ActionListener {
	/**
	 * The message edit panel which the forum list is displayed on it.
	 */
	private MessageEditorPanel messagePanel;
	
	/**
	 * The client instance which provide the service.
	 */
	private Client client;
	
	/**
	 * The group id which the forum message belongs to.
	 */
	private int groupId;

	/**
	 * The user name of user who have send this forum message
	 */
	private String userName;
	
	/**
	 * The constructor which is to construct a corresponding listener class
	 * which deal with the chat message send.
	 * 
	 * @param client
	 *            The client instance which provide the service.
	 * @param messagePanel
	 *            The message edit panel which the chat message list is displayed on
	 *            it.
	 * @param groupId
	 *            The group id which the chat message belongs to.
	 * @param userName
	 *            The user name of user who have send this chat message
	 */
	public ListenerSendLiveChatMsg(Client client,MessageEditorPanel messagePanel,int groupId,String userName) {
		this.client = client;
		this.messagePanel = messagePanel;
		this.groupId = groupId;
		this.userName = userName;
	}
	
	/**
	 * This is method is to perform the logic when a user click the message send
	 * button. Which will use CallBackServiceImpl to send this message to all
	 * the participant who currently is participanted into this group.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		JTextArea messageArea = messagePanel.getTaEditor();
		String message = messageArea.getText().trim();
		try {
			if(!message.equals("")){
				IChatService chatService = client.getChatService();
				chatService.sendChatMessage(groupId,userName, message);
				messageArea.setText("");
			} else {
				PromotDialog.errorMessageBox(null, "Error", "Chat message is not allowed empty!");
			}
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
	}

}
